package com.hl.card.mq;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hl.card.common.mq.Mq4Base;
import com.hl.card.common.mq.RabbitConfigBase;
import com.hl.card.common.persistence.dao.CreditCardMapper;
import com.hl.card.common.persistence.model.Recharge;
import com.hl.card.core.shiro.ShiroKit;
import com.hl.card.modular.pack.dao.DisMemberApplyMapper;
import com.hl.card.modular.pack.service.RechargeService;
import com.rabbitmq.client.Channel;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

@Component
@Configuration
public class Mq4NotifyDemo extends Mq4Base {
    String logPrefix = "【延时消息测试】";
    @Autowired
    private static int count = 0;
    @Autowired
    private RechargeService rechargeService;
    @Autowired
    private CreditCardMapper creditCardMapper;
    
	@Resource(name = "disMemberApplyYMapper")
	private DisMemberApplyMapper disMemberApplyMapper;
	
    public void send(String queue_name, String msg) {
        super.send(queue_name, msg);
    }

    public void send(String queue_name, String msg, long delay) {
        super.send(queue_name, msg, delay);
    }

        /*
* 监听回调
 */
    @RabbitListener(queues= MqCustomConfig.QUEUE_AWARD_ADMIN)
    public void receiveCallback(Message message, Channel channel) throws IOException {
    	String msg = StringEscapeUtils.unescapeJson(new String(message.getBody()));
    	if(msg.indexOf("\"")==0)
    	{
    	  msg=msg.substring(1,msg.length()-1);	
    	}
      	System.out.println(msg);
    	JSONObject obj=JSONObject.parseObject(msg);
    	JSONObject data=obj.getJSONObject("data");
    	String orderNo=data.getString("mchOrderNo");
    	Map<String,Object> param=new HashMap<String,Object>();
    	param.put("order_no", orderNo);
    	param.put("reward_remark", msg);
    	disMemberApplyMapper.updateRewardInfo(param);
  
    }

       @RabbitListener(queues= MqCustomConfig.QUEUE_AWARD_ADMIN_CARD)
       public void receiveCardCallback(Message message, Channel channel) throws IOException {
       	String msg = StringEscapeUtils.unescapeJson(new String(message.getBody()));
       	if(msg.indexOf("\"")==0)
       	{
       	  msg=msg.substring(1,msg.length()-1);	
       	}
        System.out.println(msg);
       	JSONObject obj=JSONObject.parseObject(msg);
       	JSONObject data=obj.getJSONObject("data");
       	String orderNo=data.getString("mchOrderNo");
       	Map<String,Object> param=new HashMap<String,Object>();
       	param.put("card_no", orderNo);
       	param.put("reward_remark", msg);
       	creditCardMapper.updateRewardInfo(param);
     
       }
    
    
    
    //监听普通消息
    @RabbitListener(queues= MqCustomConfig.TOKEN_NOTIFY_WITHDRAW_ARRIVED_QUEUE)
    public void receivePayCenterMsg(Message message, Channel channel) throws IOException {
        //channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);//消息处理成功，删除队列中消息
        String msg = StringEscapeUtils.unescapeJson(new String(message.getBody()));
        String json = msg.substring(1,msg.length()-1);
        //TODO 根据hash找到提币订单，更新订单状态为提币到账
        JSONObject obj = JSONObject.parseObject(json);

        Double gas = obj.getDouble("gas");
        String hash = obj.getString("hash");
        String detail = obj.getString("detail");
        String timeStamp = obj.getString("timeStamp");
        String blockNumber = obj.getString("blockNumber");

        Recharge rechargeSelect= new Recharge();
        rechargeSelect.setRemarks(detail);
        rechargeSelect.setDoc_account("auto_set");
        rechargeSelect.setTrans_order_id(hash);
        Recharge tempRecharge= rechargeService.selectMultiConditionOne(rechargeSelect);
        if(tempRecharge != null) {
            //将矿工费写入
            tempRecharge.setMiner(gas);
            tempRecharge.setStatus(5);
            rechargeService.auditing(tempRecharge);
        }

//        //JSONObject msgObj = JSON.parseObject(jsonStr);
        //accountTxlistService.txEthList();
    }

    //监听普通消息
    @RabbitListener(queues= MqCustomConfig.TOKEN_NOTIFY_COLLECTION_ARRIVED_QUEUE)
    public void receiveCollectionArrivedMsg(Message message, Channel channel) throws IOException {
        //channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);//消息处理成功，删除队列中消息
        String msg = StringEscapeUtils.unescapeJson(new String(message.getBody()));
        String json = msg.substring(1,msg.length()-1);
        JSONObject obj = JSONObject.parseObject(json);
        String hash = obj.getString("collection_hash");
        if(StringUtils.isNotBlank(obj.getString("collection_hash"))) {
            //归集提交成功

        }else {//归集到账
            Double gas = obj.getDouble("gas");
            hash = obj.getString("hash");
            String detail = obj.getString("detail");
            String timeStamp = obj.getString("timeStamp");
            String blockNumber = obj.getString("blockNumber");
        }

//        //JSONObject msgObj = JSON.parseObject(jsonStr);
        //accountTxlistService.txEthList();
    }
}